package com.wmh.baseservice.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.wmh.baseservice.common.utils.CommUtil;
import com.wmh.baseservice.common.utils.returnbody.ResultCodeInfo;
import com.wmh.baseservice.common.utils.returnbody.ReturnBody;
import com.wmh.baseservice.merchant.pojo.MerchantInfo;
import com.wmh.baseservice.merchant.service.MerchantInfoService;
import com.wmh.baseservice.system.pojo.SysRedEnvelope;
import com.wmh.baseservice.user.pojo.UsInfo;
import com.wmh.baseservice.user.service.UsInfoService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.wmh.baseservice.system.service.SysRedEnvelopeService;

import java.math.BigDecimal;

/**
 * @author wmh generator
 * @since 2022-08-02
 */
@Api(tags = "系统-红包相关接口")
@RestController
@RequestMapping(value = "system/sysRedEnvelope", produces = "text/plain;charset=utf-8")
public class SysRedEnvelopeController {

    @Resource
    private SysRedEnvelopeService sysRedEnvelopeService;
    @Resource
    private HttpServletRequest request;
    @Resource
    private MerchantInfoService merchantInfoService;
    @Resource
    private UsInfoService usInfoService;

    @ApiOperation("红包详情")
    @PostMapping("detail")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataTypeClass = Long.class, name = "id", value = "id", required = false),
    })
    public String detail(Long rid) {
        String type = "";
        SysRedEnvelope sysRedEnvelope = sysRedEnvelopeService.getByIdForUpdate(rid);
        if (!CommUtil.checkNull(sysRedEnvelope)) {
            return ReturnBody.error(ResultCodeInfo.SERVICE_EXCEPTION);
        }
        Long usId = (Long) request.getAttribute("usId");
        if (CommUtil.checkNull(usId)) {
            if (!sysRedEnvelope.getSendId().equals(usId) && !sysRedEnvelope.getReceiveId().equals(usId)) {
                return ReturnBody.error("无权操作");
            }
            type = "user";
        } else {
            Long id = (Long) request.getAttribute("id");
            if (!sysRedEnvelope.getSendId().equals(id) && !sysRedEnvelope.getReceiveId().equals(id)) {
                return ReturnBody.error("无权操作");
            }
            type = "merchant";
        }
        //如果未领取，并且是对方点击红包，那么领取
        if (sysRedEnvelope.getStatus() == 0) {
            if (sysRedEnvelope.getSendType() == 0 && "user".equals(type)) {
                sysRedEnvelopeService.receive(sysRedEnvelope);
            }
            if (sysRedEnvelope.getSendType() == 1 && "merchant".equals(type)) {
                sysRedEnvelopeService.receive(sysRedEnvelope);
            }
        }
        JSONObject object = new JSONObject(true);
        object.put("rid", sysRedEnvelope.getId());
        if (sysRedEnvelope.getSendType() == 0) {
            //商家发的红包
            MerchantInfo merchantInfo = merchantInfoService.getByIdForUpdate(sysRedEnvelope.getSendId());
            if (!CommUtil.checkNull(merchantInfo)) {
                return ReturnBody.error(ResultCodeInfo.SERVICE_EXCEPTION);
            }
            object.put("from", merchantInfo.getStoreName());
            object.put("face", merchantInfo.getStoreAvatar());
        } else {
            UsInfo usInfo = usInfoService.getByIdForUpdate(sysRedEnvelope.getSendId());
            if (!CommUtil.checkNull(usInfo)) {
                return ReturnBody.error(ResultCodeInfo.SERVICE_EXCEPTION);
            }
            object.put("from", usInfo.getNick());
            object.put("face", usInfo.getAvatar());
        }
        object.put("type", sysRedEnvelope.getSendType());
        object.put("money", sysRedEnvelope.getMoney());
        object.put("status", sysRedEnvelope.getStatus());
        object.put("blessing", sysRedEnvelope.getRemark());
        return ReturnBody.success(object);
    }

    @ApiOperation("发红包")
    @PostMapping("sendRedEnvelopeData")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataTypeClass = Long.class, name = "id", value = "id", required = false),
    })
    public String sendRedEnvelopeData(String blessing, BigDecimal money, Long id) {
        if (money.compareTo(BigDecimal.ZERO) <= 0) {
            return ReturnBody.error("请输入正确的金额");
        }
        Long usId = (Long) request.getAttribute("usId");
        SysRedEnvelope sysRedEnvelope = new SysRedEnvelope().setRemark(blessing).setMoney(money).setReceiveId(id);
        if (CommUtil.checkNull(usId)) {
            //查询用户余额
            UsInfo usInfo = usInfoService.getByIdForUpdate(usId);
            if (usInfo.getBalance().compareTo(money) < 0) {
                return ReturnBody.error("余额不足，当前余额为:" + usInfo.getBalance());
            }
            sysRedEnvelope.setSendId(usId).setSendType(1);
        } else {
            Long merchantId = (Long) request.getAttribute("id");
            //查询商家余额-
            MerchantInfo merchantInfo = merchantInfoService.getByIdForUpdate(merchantId);
            if (merchantInfo.getBalance().compareTo(money) < 0) {
                return ReturnBody.error("余额不足，当前余额为:" + merchantInfo.getBalance());
            }
            sysRedEnvelope.setSendId(merchantId).setSendType(0);
        }
        sysRedEnvelopeService.sendRedEnvelopeData(sysRedEnvelope);
        return ReturnBody.success(sysRedEnvelope.getId());
    }

}
